<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-7-27
 * Time: 上午10:45
 */
namespace API;

use DB\Article;
use DB\ArticleInfo;

//require_once("../db/Article.inc");
//require_once("../config/Time.inc");

class ArticleOperateResponseItem{
  /**
   * @var string
   */
  public $id;
  /**
   * @var string
   */
  public $name;

  public function __construct(string $id,string $name) {
    $this->id = $id;
    $this->name = $name;
  }
}

class ArticleOperate{

  public function run(){
    var_dump($this->getList());
  }

  private function insert():string {
    $info = new ArticleInfo();
    $article = new Article();
    $array = ["title" => "java全解析","author" => "钱有才",
      "description" => "这是一本有着非常多有意义的内容的一本书啊"];
    foreach ($array as $k => $v){
      $info->{$k} = $v;
    }
    $result = $article->insert($info)? "insert success":"insert failure";
    return $result;
  }

  private function getList():array {
    $article = new Article();
    $cursor = $article->getMany();
    $result = [];
    foreach ($cursor as $r){
      /**
       * @var $r ArticleInfo
       */
      $result[] = new ArticleOperateResponseItem($r->_id,$r->title);
    }
    return $result;
  }

  private function update():string {
    $info = new ArticleInfo();
    $article = new Article();
    $info->_id = "5b5a90b31259c610cb5f38c1";
    $info->title = "C语言入门基础";
    $info->content = "C语言入门基础内容实在是想不出来了";
    $result = $article->update($info) ? "update success" : "update error";
    return $result;
  }

  private function delete():string {
    $info = new ArticleInfo();
    $artile = new Article();
    $info->_id = "5b5a90b31259c610cb5f38c1";
    $result = $artile->delete($info) ? "delete success" : "delete error";
    return $result;
  }

}